﻿function TechnologyViewModel(parent) {
    var self = this;

    // knockout variables
    self.technologies = ko.observableArray([]);
    self.workingTechnology = ko.observable(null);

    //PAGING + SEARCH
    self.search = ko.observable("");
    self.currentPage = ko.observable(0);
    self.pageSize = ko.observable(10);
    self.pagesCount = ko.observable(1);
    self.count = ko.observable(0);
    self.lastPage = function() {
        self.currentPage(self.pagesCount() - 1);
    };
    self.firstPage = function() {
        self.currentPage(0);
    };
    self.nextPage = function() {
        if (self.currentPage() < self.pagesCount() - 1) {
            self.currentPage(self.currentPage() + 1);
        }
        ;
    };
    self.previousPage = function() {
        if (self.currentPage() > 0) {
            self.currentPage(self.currentPage() - 1);
        }
    };

    //SELECT TECHNOLOGY
    self.selectTechnology = function(technology) {
        self.workingTechnology(technology);
    };
    self.deselectTechnology = function() {
        self.workingTechnology(null);
    };
    self.selectWorkingTechnology = function(technology) {
        self.selectTechnology(technology);
    };
    self.selectNewTechnology = function() {
        var newTechnology = new Technology({ });
        self.technologies.push(newTechnology);
        self.selectTechnology(newTechnology);
    };

    //CRUD OPERATIONS
    self.saveNewTechnology = function() {
        if (self.workingTechnology().errors().length == 0) {
            $.ajax({
                    url: "/Technology/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingTechnology()),
                    contentType: 'application/json',
                    success: function(id) {
                        self.workingTechnology().Id(id);
                        self.deselectTechnology();
                        $("#CreateTechnology").modal("hide");
                    }
                });
        }
    };
    self.saveEditTechnology = function() {
        if (self.workingTechnology().errors().length == 0) {
            $.ajax({
                    url: "/Technology/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingTechnology()),
                    contentType: 'application/json',
                    success: function() {
                        self.deselectTechnology();
                        $("#EditTechnology").modal("hide");
                    }
                });
        }
    };
    self.deleteTechnology = function() {
        $.post("/Technology/DeleteTechnology", { id: self.workingTechnology().Id() }, function() {
            self.technologies.pop(self.workingTechnology());
            self.deselectTechnology();
        });
        $('#DeleteTechnology').modal('hide');
    };
    self.cancelNewTechnology = function() {
        self.deselectTechnology();
        self.technologies.pop(self.workingTechnology());
        $('#CreateTechnology').modal('hide');
    };
    self.cancelEditTechnology = function() {
        self.loadTechnologies();
        self.deselectTechnology();
        $('#EditTechnology').modal('hide');
    };
    self.cancelDetailsTechnology = function() {
        self.deselectTechnology();
        $('#DetailsTechnology').modal('hide');
    };
    self.cancelDeleteTechnology = function() {
        self.deselectTechnology();
        $('#DeleteTechnology').modal('hide');
    };

    self.loadTechnologies = function(pageIndex, pageSize, search) {
        $.getJSON("/Technology/List", { search: search, pageIndex: pageIndex, pageSize: pageSize }, function(technologies) {
            self.pagesCount(technologies.PagesCount);
            self.count(technologies.Count);
            var mappedTechnologies = $.map(technologies.Entities, function(item) { return new Technology(item); });
            self.technologies(mappedTechnologies);
        });
    };
    self.triggerLoadTechnologies = ko.computed(function() {
        self.currentPage();
        self.pageSize();
        var search = self.search();
        if (parent.route() == "technologies") {
            var route = '#/Technologies/List/?pageIndex=' + self.currentPage() + '&pageSize=' + self.pageSize();
            if (search != null && search != "") {
                route = route + '&search=' + self.search();
            }
            location.hash = route;
        }
    }, this);

}